/**
 * 
 */
package com.seguriapp.activities;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.seguriapp.R;
import com.seguriapp.adapters.LstTypeEntityAdapter;
import com.seguriapp.controllers.EntityController;
import com.seguriapp.entities.EntityType;

/**
 * @author Pablo
 * 
 */
public class EntityTypeActivity extends BaseActivity {

	private ListView lstViewTypeEntities;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_entity_type);
		mContext = this;
		lstViewTypeEntities = ((ListView) findViewById(R.id.entity_Typelist));
		lstViewTypeEntities.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				final EntityType type = (EntityType) lstViewTypeEntities
						.getItemAtPosition(position);
				final Intent intent = new Intent(mContext, EntityActivity.class);
				intent.putExtra("typeId", type.getId());
				startActivity(intent);

			}
		});

		fillList();
	}

	private void fillList() {
		mostrarDialogoEsperaSinCancel(mContext);
		new Thread(new Runnable() {
			public void run() {
				final EntityController eController = new EntityController(
						mContext);
				final ArrayList<EntityType> items = eController
						.getEntityTypeList();

				runOnUiThread(new Runnable() {
					public void run() {
						lstViewTypeEntities
								.setAdapter(new LstTypeEntityAdapter(mContext,
										R.layout.list_entity_type_item, items));
						ocultarDialogo();
					}
				});
			}
		}).start();

	}

}
